var express = require('express');
var router = express.Router();
var { aesDecrypt } = require("../utils");
const { data_model } = require('../utils/model');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express',
    username: req.session.username,   // 用户名已经登录 
    nickname: req.session.nickname,   // 用户名已经登录 
    phone: req.session.phone,   // 用户名已经登录 
  });
});

router.get("/login", (req, res) => {
  var username = req.query.username ? aesDecrypt(req.query.username) : ''
  res.render("login", {
    username
  })
})

router.get("/register", (req, res) => {
  res.render("register")
})

router.get("/good", (req, res) => {
  data_model.find({}, {}).then(result => {
    res.render("good", { result })
  }).catch(err => {
    res.send("服务器异常-500")
  })
})

router.get("/logout", (req, res) => {
  req.session.destroy(() => {   // 销毁session 
    res.redirect("/")
  })
})

router.get("/detail", (req, res) => {
  var query = req.query
  data_model.findOne({
    _id: query.goodid
  })
    .then(result => {
      console.log(result)
      res.render("detail", { result })
    })
    .catch(err => {
      console.log(err)
      res.render("error")
    })
})



module.exports = router;
